<?php
if (!defined('RAIZ')) define('RAIZ', $_SERVER['DOCUMENT_ROOT']. '/neumo/'); 

require_once RAIZ . 'lib/aux-new.php';

/*
#############################################
# Shiege Iseng Resize Class
# 11 March 2003
# shiegege_at_yahoo.com
# View Demo :
#   http://shiege.com/scripts/thumbnail/
# Modificada por Eloy Velázquez (eloy.velazquez@gmail.com): 24-1-2012
############################################
  Sample :
  $thumb=new thumbnail("./shiegege.jpg");               // generate image_file, set filename to resize
  $thumb->size_width(100);				// set width for thumbnail, or
  $thumb->size_height(300);				// set height for thumbnail, or
  $thumb->size_auto(200);					// set the biggest width or height for thumbnail
  $thumb->jpeg_quality(75);				// [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
  $thumb->show();						// show your thumbnail
  $thumb->save("./huhu.jpg");				// save your thumbnail to file
  ----------------------------------------------
  Note :
  - GD must Enabled
  - Autodetect file extension (.jpg/jpeg, .png, .gif, .wbmp)
  but some server can't generate .gif / .wbmp file types
  - If your GD not support 'ImageCreateTrueColor' function,
  change one line from 'ImageCreateTrueColor' to 'ImageCreate'
  (the position in 'show' and 'save' function)
############################################
*/

class thumbnail {

  private $img;
  private $error;

  function thumbnail($imgfile) {
    //detect image format
    $this->error = FALSE;
    //$this->img["format"] = ereg_replace(".*\.(.*)$", "\\1", $imgfile);
    $fInfo = finfo_open(FILEINFO_MIME_TYPE);
    $sInfo = finfo_file($fInfo, $imgfile);
    $this->img["format"] = $sInfo;
    if ($this->img["format"] == "image/jpeg") {
      //JPEG
      $this->img["format"] = "JPEG";
      $this->img["src"] = ImageCreateFromJPEG($imgfile);
    } elseif ($this->img["format"] == "image/png") {
      //PNG
      $this->img["format"] = "PNG";
      $this->img["src"] = ImageCreateFromPNG($imgfile);
    } elseif ($this->img["format"] == "image/gif") {
      //GIF
      $this->img["format"] = "GIF";
      $this->img["src"] = ImageCreateFromGIF($imgfile);
    } elseif ($this->img["format"] == "image/x-ms-bmp") {
      //WBMP
      $this->img["format"] = "WBMP";
      $this->img["src"] = ImageCreateFromWBMP($imgfile);
    } else {
      //DEFAULT
      //echo "Not Supported File";
//      exit();
      $this->error = true;
      return;
    }
    @$this->img["ancho"] = imagesx($this->img["src"]);
//    @$this->img["alto"] = imagesy($this->img["src"]);
    @$this->img["alto"] = imagesy($this->img["src"]);
    //default quality jpeg
    $this->img["calidad"] = 75;
  }

  function error() {
    return $this->error;
  }

  function size_height($size = 100) {
    //height
    $this->img["alto_thumb"] = $size;
    @$this->img["ancho_thumb"] = ($this->img["alto_thumb"] / $this->img["alto"]) * $this->img["ancho"];
  }

  function size_width($size = 100) {
    //width
    $this->img["ancho_thumb"] = $size;
    @$this->img["alto_thumb"] = ($this->img["ancho_thumb"] / $this->img["ancho"]) * $this->img["alto"];
  }

  function size_auto($size = 100) {
    //size
    if ($this->img["ancho"] >= $this->img["alto"]) {
      $this->img["ancho_thumb"] = $size;
      @$this->img["alto_thumb"] = ($this->img["ancho_thumb"] / $this->img["ancho"]) * $this->img["alto"];
    } else {
      $this->img["alto_thumb"] = $size;
      @$this->img["ancho_thumb"] = ($this->img["alto_thumb"] / $this->img["alto"]) * $this->img["ancho"];
    }
  }

  function jpeg_quality($quality = 75) {
    //jpeg quality
    $this->img["calidad"] = $quality;
  }

  function show() {
    //show thumb
    @Header("Content-Type: image/" . $this->img["format"]);

    /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function */
    $this->img["des"] = ImageCreateTrueColor($this->img["ancho_thumb"], $this->img["alto_thumb"]);
    imagecopyresampled($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["ancho_thumb"], $this->img["alto_thumb"], $this->img["ancho"], $this->img["alto"]);

    if ($this->img["format"] == "JPEG") {
      //JPEG
      imageJPEG($this->img["des"], "", $this->img["calidad"]);
    } elseif ($this->img["format"] == "PNG") {
      //PNG
      imagePNG($this->img["des"]);
    } elseif ($this->img["format"] == "GIF") {
      //GIF
      imageGIF($this->img["des"]);
    } elseif ($this->img["format"] == "WBMP") {
      //WBMP
      imageWBMP($this->img["des"]);
    }
  }

  function save($save = "") {
    //save thumb
    !empty($save) or die('Error SAVE sin parámetro. ');
    //$save = strtolower("./thumb." . $this->img["format"]);
    /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function */
    $this->img["des"] = @ImageCreateTrueColor($this->img["ancho_thumb"], $this->img["alto_thumb"]) or die('Error creando la imagen ImageCreateTrueColor');
    //@imagecopyresized($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["ancho_thumb"], $this->img["alto_thumb"], $this->img["ancho"], $this->img["alto"]);
    //crea_imagen();
    @imagecopyresampled($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["ancho_thumb"], $this->img["alto_thumb"], $this->img["ancho"], $this->img["alto"]) or die('Error en imagecopyresampled ');

    if ($this->img["format"] == "JPEG") {
      //JPEG
      imageJPEG($this->img["des"], "$save", $this->img["calidad"]);
    } elseif ($this->img["format"] == "PNG") {
      //PNG
      imagePNG($this->img["des"], "$save");
    } elseif ($this->img["format"] == "GIF") {
      //GIF
      imageGIF($this->img["des"], "$save");
    } elseif ($this->img["format"] == "WBMP") {
      //WBMP
      imageWBMP($this->img["des"], "$save");
    }
  }

}

class thumb_dir extends thumbnail {

  private $rsl = array();

  function thumb_dir($dir_origen, $dir_destino, $alto = 50, $ancho = 50) {
    @is_dir($dir_origen) or die('Error directorio no existe: ' . $dir_origen);
    if (!@is_dir($dir_destino)) {
      @mkdir($dir_destino) or die('Error al crear el directorio: ' . $dir_destino);
    } else {
      fichero_borra_directorio_contenido($dir_destino);
    }
    foreach (glob($dir_origen . "*.*") as $archivo) {
      $arc_nombre = fichero_extrae_nombre($archivo);
      $mini_nombre = $dir_destino . $arc_nombre;
      $mini = new thumbnail($archivo);
      if (!$mini->error()) { //detecta error de formato
        $mini->size_height($alto);
        $mini->save($mini_nombre);
        $this->rsl['archivo'][] = $arc_nombre;
//        $this->rsl['mini'][] = $mini_nombre;
      }
    }
    if (count($this->rsl) == 0) {
      $this->rsl['error'] = TRUE;
      $this->rsl['msj'] = 'No hay imagenes para convertir';
    } else {
      $this->rsl['error'] = FALSE;
      $this->rsl['msj'] = 'ok';
    }
  }

  function toma_resultado() {
    return $this->rsl;
  }

}

function imagen_rezise($imgfile,$destino ,$altura) {
  $fInfo = finfo_open(FILEINFO_MIME_TYPE);
  $sInfo = finfo_file($fInfo, $imgfile);
  if (($sInfo = "image/jpeg") || ($sInfo = "png")) {
   $alto= GetImageSize($imgfile);
    if ($alto[1] > $altura ){
      $img_temp= new thumbnail($imgfile);
      if (!$img_temp->error()){
        $img_temp->size_height($altura);
        $img_temp->save($destino);
      }else return false;
    }else{ 
      if (!copy($imgfile, $destino)){
        return FALSE;
      }
    }
  }else return false;  
  return true;
}

?>